#!/bin/tcsh -ef
#############################################################################
#                                                                           #
# Title: Copy Merged Dataset                                                #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 03/01/2006                                             #
# Last Modification: 03/01/2006                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 30
#
# MANUAL: This script allows to copy the most recent merged dataset onto one of the 9 registers.
#
# MANUAL: Use this script to savegard the result of the lastes merging process.
#
# DISPLAY: merge_copy_onto
# DISPLAY: merge_ref_num
# DISPLAY: merge_comment_1
# DISPLAY: merge_register_date_1
# DISPLAY: merge_lock_1
# DISPLAY: merge_comment_2
# DISPLAY: merge_register_date_2
# DISPLAY: merge_lock_2
# DISPLAY: merge_comment_3
# DISPLAY: merge_register_date_3
# DISPLAY: merge_lock_3
# DISPLAY: merge_comment_4
# DISPLAY: merge_register_date_4
# DISPLAY: merge_lock_4
# DISPLAY: merge_comment_5
# DISPLAY: merge_register_date_5
# DISPLAY: merge_lock_5
# DISPLAY: merge_comment_6
# DISPLAY: merge_register_date_6
# DISPLAY: merge_lock_6
# DISPLAY: merge_comment_7
# DISPLAY: merge_register_date_7
# DISPLAY: merge_lock_7
# DISPLAY: merge_comment_8
# DISPLAY: merge_register_date_8
# DISPLAY: merge_lock_8
# DISPLAY: merge_comment_9
# DISPLAY: merge_register_date_9
# DISPLAY: merge_lock_9
#
#$end_local_vars
#
set proc_2dx = ""
set bin_2dx = ""
set merge_copy_onto = ""
set merge_comment_1 = ""
set merge_comment_2 = ""
set merge_comment_3 = ""
set merge_comment_4 = ""
set merge_comment_5 = ""
set merge_comment_6 = ""
set merge_comment_7 = ""
set merge_comment_8 = ""
set merge_comment_9 = ""
set merge_lock_1 = ""
set merge_lock_2 = ""
set merge_lock_3 = ""
set merge_lock_4 = ""
set merge_lock_5 = ""
set merge_lock_6 = ""
set merge_lock_7 = ""
set merge_lock_8 = ""
set merge_lock_9 = ""
#
#$end_vars
#
set scriptname = 2dx_copyMerge
\rm -f LOGS/${scriptname}.results
#
set ccp4_setup = 'y'
source ${proc_2dx}/initialize
#
echo merge_copy_onto = ${merge_copy_onto}
echo "<<@progress: 1>>"
#
if ( ${merge_copy_onto} == "0" ) then
  #
  #############################################################################
  ${proc_2dx}/linblock "Not doing anything."
  #############################################################################
  #
else
  #
  set merge_lock = n
  if ( ${merge_copy_onto} == "1" ) then
    if ( ${merge_lock_1} == "y" ) then
      set merge_lock = y
    endif
  endif
  if ( ${merge_copy_onto} == "2" ) then
    if ( ${merge_lock_2} == "y" ) then
      set merge_lock = y
    endif
  endif
  if ( ${merge_copy_onto} == "3" ) then
    if ( ${merge_lock_3} == "y" ) then
      set merge_lock = y
    endif
  endif
  if ( ${merge_copy_onto} == "4" ) then
    if ( ${merge_lock_4} == "y" ) then
      set merge_lock = y
    endif
  endif
  if ( ${merge_copy_onto} == "5" ) then
    if ( ${merge_lock_5} == "y" ) then
      set merge_lock = y
    endif
  endif
  if ( ${merge_copy_onto} == "6" ) then
    if ( ${merge_lock_6} == "y" ) then
      set merge_lock = y
    endif
  endif
  if ( ${merge_copy_onto} == "7" ) then
    if ( ${merge_lock_7} == "y" ) then
      set merge_lock = y
    endif
  endif
  if ( ${merge_copy_onto} == "8" ) then
    if ( ${merge_lock_8} == "y" ) then
      set merge_lock = y
    endif
  endif
  if ( ${merge_copy_onto} == "9" ) then
    if ( ${merge_lock_9} == "y" ) then
      set merge_lock = y
    endif
  endif
  #
  if ( ${merge_lock} == "y" ) then
    ${proc_2dx}/linblock "Not running, since register ${merge_copy_onto} is locked."
  else
    #
    #############################################################################
    ${proc_2dx}/linblock "Copying latest result onto register ${merge_copy_onto}."
    #############################################################################
    #
    if ( ! -d REGISTERS ) then
      \mkdir REGISTERS
    endif
    cd REGISTERS
    \rm -rf Reg_${merge_copy_onto}
    \mkdir Reg_${merge_copy_onto}
    cd ..
    if ( -e merge2D_MRClefthanded.mtz ) then
      \cp -f merge2D_MRClefthanded.mtz REGISTERS/Reg_${merge_copy_onto}
      ${proc_2dx}/linblock "merge2D_MRClefthanded.mtz copied."
    else
      ${proc_2dx}/linblock "WARNING: merge2D_MRClefthanded.mtz not found."
    endif
    if ( -e merge3D_MRClefthanded.mtz ) then
      \cp -f merge3D_MRClefthanded.mtz REGISTERS/Reg_${merge_copy_onto}
      ${proc_2dx}/linblock "merge3D_MRClefthanded.mtz copied."
    else
      ${proc_2dx}/linblock "WARNING: merge3D_MRClefthanded.mtz not found."
    endif
    if ( -e merge3Dref_MRClefthanded.mtz ) then
      \cp -f merge3Dref_MRClefthanded.mtz REGISTERS/Reg_${merge_copy_onto}
      ${proc_2dx}/linblock "merge3Dref_MRClefthanded.mtz copied."
    else
      ${proc_2dx}/linblock "WARNING: merge3Dref_MRClefthanded.mtz not found."
    endif
    if ( -e APH/merge.aph ) then
      \cp -f APH/merge.aph REGISTERS/Reg_${merge_copy_onto}
      ${proc_2dx}/linblock "APH/merge.aph copied."
    else
      ${proc_2dx}/linblock "WARNING: APH/merge.aph not found."
    endif
    set ref_date = `date | sed 's/ /_/g'`
    echo "#unlock merge_register_date_${merge_copy_onto}" >> LOGS/${scriptname}.results
    echo "set merge_register_date_${merge_copy_onto} =" \"${ref_date}\" >> LOGS/${scriptname}.results
    echo "#lock merge_register_date_${merge_copy_onto}" >> LOGS/${scriptname}.results
    echo "Creation date: ${ref_date}" > REGISTERS/Reg_${merge_copy_onto}/COMMENT.txt
    if ( ${merge_copy_onto} == "1" ) then
      echo "Comment: ${merge_comment_1}" >> REGISTERS/Reg_${merge_copy_onto}/COMMENT.txt
    endif
    if ( ${merge_copy_onto} == "2" ) then
      echo "Comment: ${merge_comment_2}" >> REGISTERS/Reg_${merge_copy_onto}/COMMENT.txt
    endif
    if ( ${merge_copy_onto} == "3" ) then
      echo "Comment: ${merge_comment_3}" >> REGISTERS/Reg_${merge_copy_onto}/COMMENT.txt
    endif
    if ( ${merge_copy_onto} == "4" ) then
      echo "Comment: ${merge_comment_4}" >> REGISTERS/Reg_${merge_copy_onto}/COMMENT.txt
    endif
    if ( ${merge_copy_onto} == "5" ) then
      echo "Comment: ${merge_comment_5}" >> REGISTERS/Reg_${merge_copy_onto}/COMMENT.txt
    endif
    if ( ${merge_copy_onto} == "6" ) then
      echo "Comment: ${merge_comment_6}" >> REGISTERS/Reg_${merge_copy_onto}/COMMENT.txt
    endif
    if ( ${merge_copy_onto} == "7" ) then
      echo "Comment: ${merge_comment_7}" >> REGISTERS/Reg_${merge_copy_onto}/COMMENT.txt
    endif
    if ( ${merge_copy_onto} == "8" ) then
      echo "Comment: ${merge_comment_8}" >> REGISTERS/Reg_${merge_copy_onto}/COMMENT.txt
    endif
    if ( ${merge_copy_onto} == "9" ) then
      echo "Comment: ${merge_comment_9}" >> REGISTERS/Reg_${merge_copy_onto}/COMMENT.txt
    endif
    #
    echo "set merge_copy_onto = 0" >> LOGS/${scriptname}.results
  endif
  #
  ${proc_2dx}/linblock "${scriptname} finished correctly."
  #
endif
#
echo "<<@progress: 100>>"
#
